package parser

import (
	"gitee.com/volvoxc60/go-crawler/engine"
	"gitee.com/volvoxc60/go-crawler/model/zhenai"
	"regexp"
	"strconv"
)

var ageRe = regexp.MustCompile(`<div data-v-8b1eac0c="" class="m-btn purple">([\d]+)岁</div>`)
var marriageRe = regexp.MustCompile(`<div data-v-8b1eac0c="" class="m-btn purple">([.*]+)岁</div>`)

func ParseProfile(contents []byte, name string) (result engine.ParseResult) {
	profile := zhenai.Profile{}
	profile.Name = name
	age, err := strconv.Atoi(extractString(contents, ageRe))
	marriage := extractString(contents, marriageRe)
	if err != nil {
		panic(err)
	}
	profile.Age = age
	profile.Marriage = marriage
	result.Items = []interface{}{profile}
	return result
}

func extractString(content []byte, re *regexp.Regexp) string {
	match := re.FindSubmatch(content)
	if len(match) >= 2 {
		return string(match[1])
	} else {
		return ""
	}
}
